package org.softmed.ATComm.swingClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;

public class ClientManager {

	RESTClient client = new DefaultRESTletClient();
	private Client gui;
	String filename = "client-config.txt";
	private String address;
	private String port;
	private List<String> ports;

	public ClientManager(Client ui) {
		this.gui = ui;
	}

	public void setup() {
		try {
			readConfig();
			client.setURL(address);
			ports = client.getSerialPorts();
			gui.setPorts(ports);
			gui.setPort(port);

			if (!ports.contains(port)) {
				gui
				.setStatus("Serial port was not in list. Check if mobile is connected to server.");
				return;
			}

		} catch (Throwable t) {
			t.printStackTrace();
			gui.setStatus("Error setting up");
			return;
		}
		gui.setStatus("success setting up");
	}

	public void readServerPortConfig() {
		try {
			address = gui.getURL();
			client.setURL(address);
			ports = client.getSerialPorts();
			gui.setPorts(ports);
			port = client.getSerialPortName();
			gui.setPort(port);

			if (!ports.contains(port)) {
				gui
						.setStatus("Serial port was not in list. Check if mobile is connected to server.");
				return;
			}

		} catch (Throwable t) {
			t.printStackTrace();
			gui
					.setStatus("Error reading serial port configuration from server");
			return;
		}
		gui.setStatus("success reading serial port configuration from server");
	}

	public void sendMessage() {
		try {
			String number = gui.getNumber();
			String message = gui.getMessage();

			address = gui.getURL();
			port = gui.getPort();

			client.setURL(address);
			client.setSerialPortName(port);
			client.sendSMS(number, message);
		} catch (Throwable t) {
			t.printStackTrace();
			gui.setStatus(t.getMessage());
			return;
		}
		gui.setStatus("Message Sent");
	}

	public void readConfig() {
		try {
			File file = new File(filename);
			if (!file.exists())
				throw new RuntimeException("File " + filename
						+ " doesn't exist!!!");

			BufferedReader reader = new BufferedReader(new FileReader(file));
			reader.readLine();
			address = reader.readLine();
			reader.readLine();
			port =reader.readLine();
//			 reader.readLine();

			reader.close();

			gui.setURL(address);
			gui.setPort(port);

		} catch (Throwable t) {
			t.printStackTrace();
			gui.setStatus("Error reading config");
			return;
		}
		gui.setStatus("Success reading config");
	}

	public void saveConfig() {
		try {
			File file = new File(filename);
			if (!file.exists())
				file.createNewFile();

			PrintWriter writer = new PrintWriter(new FileWriter(file));
			writer.write("http address:\n");
			address = gui.getURL();
			writer.write(address);
			writer.write("\n");
			writer.write("Serial Port:\n");
			port = gui.getPort();
			writer.write(port);

			writer.close();

		} catch (Throwable t) {
			t.printStackTrace();
			gui.setStatus("Error saving config");
			return;
		}
		gui.setStatus("Success saving config");
	}

}
